package com.ruoyi.edu.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.edu.domain.Risk;
import com.ruoyi.edu.service.IRiskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;

/**
 * 风险权重Controller
 * 
 * @author czdu
 * @date 2022-11-09
 */
@RestController
@RequestMapping("/edu/risk")
public class RiskController extends BaseController
{
    @Autowired
    private IRiskService riskService;

    /**
     * 查询风险权重列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Risk risk)
    {
        startPage();
        List<Risk> list = riskService.selectRiskList(risk);
        return getDataTable(list);
    }

    /**
     * 导出风险权重列表
     */
    @Log(title = "风险权重", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Risk risk)
    {
        List<Risk> list = riskService.selectRiskList(risk);
        ExcelUtil<Risk> util = new ExcelUtil<Risk>(Risk.class);
        util.exportExcel(response, list, "风险权重数据");
    }

    /**
     * 获取风险权重详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(riskService.selectRiskById(id));
    }

    /**
     * 新增风险权重
     */
    @Log(title = "风险权重", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Risk risk)
    {
        return toAjax(riskService.insertRisk(risk));
    }

    /**
     * 修改风险权重
     */
    @Log(title = "风险权重", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Risk risk)
    {
        Risk risk1 = new Risk();
        risk1.setLevel(risk.getLevel());
        risk1.setCategory(risk.getCategory());
        riskService.updateRisk(risk);
        List<Risk> risks = riskService.selectRiskList(risk1);
        BigDecimal total = new BigDecimal(Double.toString(0));
        for (Risk rk:risks) {
            total = total.add(new BigDecimal(Double.toString(rk.getWeight()))) ;
        }
        return AjaxResult.success(total);
    }

    /**
     * 删除风险权重
     */
    @Log(title = "风险权重", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(riskService.deleteRiskByIds(ids));
    }
}
